package com.wthink.newMain.settings.data.inner

import android.content.Context
import android.graphics.Color
import android.os.Bundle
import android.speech.tts.TextToSpeech
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.TextView
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import com.wthink.hzgj.R
import com.wthink.hzgj.databinding.FragmentDataPortBinding
import com.wthink.hzgj.util.`interface`.NoticeCallBack
import com.wthink.newMain.data.SerialData
import com.wthink.newMain.settings.data.inner.viewmodel.DataPortViewmodel
import kotlinx.android.synthetic.main.fragment_data_port.*


class DataPortFragment : Fragment() {


    private val viewModel by viewModels<DataPortViewmodel>()

    private lateinit var binding: FragmentDataPortBinding

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = DataBindingUtil.inflate(
            inflater,
            R.layout.fragment_data_port
            ,
            container,
            false
        )
        binding.viewModel = viewModel
        binding.lifecycleOwner = this
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        viewModel.start{baudrateList,serialList->
            activity?.runOnUiThread {
                serialList.forEachIndexed { index, serialData ->
                    val indexOf = baudrateList.indexOf(serialData.baudRate)
                    when (index) {
                        0 -> {
                            spinner1.setSelection(indexOf)
                        }
                        1 -> {
                            spinner2.setSelection(indexOf)
                        }
                        2 -> {
                            spinner3.setSelection(indexOf)
                        }
                        3 -> {
                            spinner4.setSelection(indexOf)
                        }
                        4 -> {
                            spinner5.setSelection(indexOf)
                        }
                    }
                }

            }

        }

/*
        viewModel.setReqCall(object : DataPortViewmodel.reqCallBack {
            override fun call(baudrateList: List<String>, serialList: List<SerialData>) {
            }
        })
*/


        binding.spinner1.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onNothingSelected(parent: AdapterView<*>?) {

            }

            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View?,
                position: Int,
                id: Long
            ) {
                viewModel.baudRate1.postValue(viewModel.baudrateList.value!![position])
            }
        }

        binding.spinner2.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onNothingSelected(parent: AdapterView<*>?) {

            }

            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View?,
                position: Int,
                id: Long
            ) {
                viewModel.baudRate2.postValue(viewModel.baudrateList.value!![position])
            }
        }
        binding.spinner3.onItemSelectedListener = object :
            AdapterView.OnItemSelectedListener {
            override fun onNothingSelected(parent: AdapterView<*>?) {

            }

            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View?,
                position: Int,
                id: Long
            ) {
                viewModel.baudRate3.postValue(viewModel.baudrateList.value!![position])
            }
        }

        binding.spinner4.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onNothingSelected(parent: AdapterView<*>?) {

            }

            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View?,
                position: Int,
                id: Long
            ) {
                viewModel.baudRate4.postValue(viewModel.baudrateList.value!![position])
            }
        }

        binding.spinner5.onItemSelectedListener = object :
            AdapterView.OnItemSelectedListener {
            override fun onNothingSelected(parent: AdapterView<*>?) {

            }

            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View?,
                position: Int,
                id: Long
            ) {
                viewModel.baudRate5.postValue(viewModel.baudrateList.value!![position])
            }
        }

        binding.commit.setOnClickListener {
            viewModel.config()
        }
    }
}